package com.genesis.yunnanji.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.genesis.yunnanji.Constants;
import com.genesis.yunnanji.R;
import com.genesis.yunnanji.adapter.ClassifyBrandAdapter;
import com.genesis.yunnanji.bean.BaseBean;
import com.genesis.yunnanji.bean.ClassifyChildBean;
import com.genesis.yunnanji.utils.NetUtils;
import com.genesis.yunnanji.widget.RecycleViewDivider;
import com.youth.xframe.widget.XToast;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.CallBackProxy;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import okhttp3.RequestBody;

/**
 * 创建日期：2018/9/30 003011:08
 * 作者：Kite
 * 描述：分类品牌
 */
public class ClassifyBrandFragment extends BaseFragment {

    @BindView(R.id.rv_brand)
    RecyclerView rvBrand;

    private ClassifyBrandAdapter mClassifyBrandAdapter;

    private List<ClassifyChildBean.ListBean> mClassifyChildBeanList = new ArrayList<>();

    /**
     * Fragment 实例
     */
    public static ClassifyBrandFragment newInstance(Object... pramars) {
        ClassifyBrandFragment fragment = new ClassifyBrandFragment();
        fragment.setArguments(getBundle(pramars));
        return fragment;
    }

    @Override
    public View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_classify_brand, container, false);
        return view;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        getBrandList();
    }

    private void dataResource() {
        rvBrand.setLayoutManager(new LinearLayoutManager(getActivity()));
        mClassifyBrandAdapter = new ClassifyBrandAdapter(getActivity(), rvBrand, mClassifyChildBeanList);
        rvBrand.setAdapter(mClassifyBrandAdapter);
        rvBrand.setItemAnimator(new DefaultItemAnimator());
        rvBrand.addItemDecoration(new RecycleViewDivider(getActivity(), LinearLayoutManager.VERTICAL, 24, getResources().getColor(R.color.color_fff)));
    }

    private void getBrandList() {
        RequestBody requestBody = NetUtils.initNet(null);
        EasyHttp.post(Constants.INDEX_BRAND)
//                .baseUrl(Constants.MAIN_ADMIN)
                .requestBody(requestBody)
                .execute(new CallBackProxy<BaseBean<ClassifyChildBean>, ClassifyChildBean>(new SimpleCallBack<ClassifyChildBean>() {
                    @Override
                    public void onError(ApiException e) {
                        XToast.error(e.getMessage());
                    }

                    @Override
                    public void onSuccess(ClassifyChildBean bean) {
                        mClassifyChildBeanList = bean.getList();
                        dataResource();
                    }
                }) {
                });
    }

}
